class EventCenter {
  constructor() {
    this.handler = {}
  }
  addEventListener (type, fn) {
    if (!this.handler[type]) {
      this.handler[type] = []
    }
    this.handler[type].push(fn)
  }
  dispatchEvent (type, ...params) {
    if (!this.handler[type]) {
      return new Error('错误')
    }
    this.handler[type].forEach((fn) => {
      fn(...params)
    })
  }
  removeEventListener (type, fn) {
    if (!this.handler[type]) {
      return new Error('错误')
    }
    const index = this.handler[type].findIndex(el => el === fn)
    if (index === -1) {
      return new Error('错误')
    } else {
      this.handler[type].splice(index, 1)
      if (this.handler[type].length === 0) {
        delete this.handler[type]
      }
    }
  }
}

let moshi = new EventCenter()
let fn = (...params) => {
  console.log('nihao');
  console.log(params);
}
moshi.addEventListener('你好', fn)
moshi.dispatchEvent('你好', 1)
moshi.removeEventListener('你好', fn)
moshi.dispatchEvent('你好', 1)
